﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RestaurantBooker.View
{
    public partial class ModalDialogContainer : ChildWindowContainer
    {
        #region Properties

        /// <summary>
        /// Validation process - will be callde then dialog is closing by Ok button pressing
        /// If returned value will be true - dialog will be closed
        /// </summary>
        public Func<ModalDialogContainer, bool> ValidationProc
        {
            protected get;
            set;
        }

        
        public Action<bool?> ConfirmationResultProc
        {
            protected get;
            set;
        }


       

        public UIElement DialogContent
        {
            get
            {
                return this.ContentBorder.Child;
            }
            set
            {
                this.ContentBorder.Child = value;
            }
        }


        /// <summary>
        /// The <see cref="ErrorMessage" /> dependency property's name.
        /// </summary>
        public const string ErrorMessagePropertyName = "ErrorMessage";

        /// <summary>
        /// Gets or sets the value of the <see cref="ErrorMessage" />
        /// property. This is a dependency property.
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return (string)GetValue(ErrorMessageProperty);
            }
            set
            {
                SetValue(ErrorMessageProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="ErrorMessage" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty ErrorMessageProperty = DependencyProperty.Register(
            ErrorMessagePropertyName,
            typeof(string),
            typeof(ModalDialogContainer),
            new PropertyMetadata(null, (dp, e) =>
                {
                    var mdc = dp as ModalDialogContainer;
                    if (mdc != null)
                    {
                        mdc.ErrorMessageTextBlock.Text = e.NewValue != null ? e.NewValue.ToString() : null;
                        if (e.NewValue == null)
                        {
                            mdc.ErrorMessageTextBlock.Visibility = Visibility.Collapsed;
                        }
                        else
                        {                        
                            mdc._hideAnimation.Stop();                        
                            //Start Showing animation
                            foreach (var tl in mdc._hideAnimation.Children)
                                Storyboard.SetTarget(tl, mdc.ErrorMessageTextBlock);
                            mdc._hideAnimation.Begin();
                        }
                    }
                }));

        #endregion


        public ModalDialogContainer()
        {
            InitializeComponent();
            _hideAnimation = this.Resources["HideAnimation"] as Storyboard;
        }


        #region Private members

        Storyboard _hideAnimation = null;


        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                CloseWindow(false);
        }

        private void CloseWindow(bool dialogResult)
        {
            if (dialogResult)
            {
                ErrorMessage = null;
                if (ValidationProc != null)
                    if (!ValidationProc(this))
                        return;
            }

            this.DialogResult = dialogResult;            
            CallFinishDelegate();
        }

        private void CallFinishDelegate()
        {
            if (ConfirmationResultProc != null)
                ConfirmationResultProc(DialogResult);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            CloseWindow(true);
        }
        #endregion

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            CloseWindow(false);
        }

    }
}
